package com.rybbaby.tss.core.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExportUtil {


    /**
     * 导出报表工具类
     * @Param path : json文件路径
     * @Param templateName 导出的文件名 (.xlsx结尾)
     * @Param List<实体类泛型> : List集合
     * @Param request : 当前类的request
     * @Param response : 当前类的response
     * */

    public void export(String path,String templateName, List<?> list, HttpServletRequest request, HttpServletResponse response){
        try {
            //调用方法，导出数据库数据
            if (list != null && !list.isEmpty()) {
                InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
                //InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
                File tempJsonFile = File.createTempFile(SequenceUtil.getLowerCaseUUID(), null);
                FileUtils.copyInputStreamToFile(inputStream, tempJsonFile);
                IOUtils.closeQuietly(inputStream);
                //将数据导出到Excel文件
                Workbook workbook = ExcelUtil.exportExcel(list, tempJsonFile);
                try {
                    tempJsonFile.delete();
                } catch (Exception e) {
                    tempJsonFile.deleteOnExit();
                }
                if (StringUtils.isNotBlank(templateName)) {
                    String suffix = org.apache.commons.lang.StringUtils.substringAfterLast(templateName, ".");
                    String contentType = request.getSession().getServletContext().getMimeType("xx." + suffix);
                    response.setContentType(contentType);
                } else {
                    templateName = SequenceUtil.getLowerCaseUUID();
                    response.setContentType("application/vnd.ms-excel");
                }
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(templateName, StandardCharsets.UTF_8.toString()));
                response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
                ServletOutputStream outputStream = response.getOutputStream();
                workbook.write(outputStream);
                IOUtils.closeQuietly(outputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
